<?php
/**
*
* class AjaxPaging
*
*/
class Paging {

    /**
     * Get string paging.
     *
     *
     *
     * @param int          $currentPage      current page.
     * @param int          $totalPages       total pages recieved from DB.
     * @param string       $link             link you want to call.
     * @param int          $numberPageView   number of num you want to view on the left or right of current page
     *                          example
     *                          $numberPageView = 3
     *                          string paging will be 1 2 3 4 5 6 7.
     * @param string       $tagID            content will send to tagID.
     * @param string       $class            css you want to use.
     * @param string      $showLoading      show char loading or not.
     * @return  string paging.
     */
    function getPaging($currentPage, $totalPages, $link, $numberPageView, $tagID, $class = 'paging', $showLoading = 'true', $params = '', $method = 'POST')
    {
		if(!(is_numeric($totalPages) && ($totalPages > 1))) return "";
        if(!is_numeric($currentPage) || ($currentPage <= 0)) $currentPage = 1;
        if($currentPage > $totalPages) $currentPage = $totalPages;

        if($totalPages < $numberPageView) $numberPageView = $totalPages;

        // Lay vi tri o giua cua so trang hien thi
  		$itemCenter = floor($numberPageView/2);
     	if($currentPage == 1)
      	{
      		$iStart = 1;
	    	$iStop = $numberPageView;
      	}
      	else
      	{
	      	if($currentPage - $itemCenter >0)
	      	{
	      		$iStart = $currentPage - $itemCenter;
	      		$iStop =  $currentPage + $itemCenter;
	      		if($iStop > $totalPages)
	      		{
	      			$iStop = $totalPages;
	      			$iStart = $totalPages - $numberPageView + 1;
	      			if($iStart <= 0) $iStart = 1;
	      		}
	      	}
	      	else
	      	{
	      		$iStart = 1;
	      		$iStop = $numberPageView;
	      	}
      	}

        /*// First
        $result = "<span style='padding:2px;'><a class='$class' href=\"javascript:showTagAjax('$tagID','$link/page/1', '$params', $showLoading, '$method')\"><<</a></span>";
   		for($i=$iStart; $i<=$iStop;$i++)
   		{
   			if($currentPage == $i)
				$result .= "<span class='$class-current'>" . $i . "</span>";
   			else
               	$result .= "<span style='padding:2px;'><a class='$class' href=\"javascript:showTagAjax('$tagID','$link/page/". $i ."','$params',$showLoading, '$method')\">" . $i . "</a></span>";
   		}
		// Last
		$result .= "<span style='padding:2px;'><a class='$class' href=\"javascript:showTagAjax('$tagID','$link/page/$totalPages','$params', $showLoading, '$method')\">>></a></span>";*/

      	$strTmp = '?';
      	if (stristr($link, '?'))
      	    $strTmp = '&';
        // First
        $result = "<span style='padding:2px;'><a id='paging' class='$class' href=\"{$link}{$strTmp}page=1\"><<</a></span>";
   		for($i=$iStart; $i<=$iStop;$i++)
   		{
   			if($currentPage == $i)
				$result .= "<span class='$class-current'>" . $i . "</span>";
   			else
               	$result .= "<span style='padding:2px;'><a id='paging' class='$class' href=\"{$link}{$strTmp}page=". $i ."\">" . $i . "</a></span>";
   		}
		// Last
		$result .= "<span style='padding:2px;'><a id='paging' class='$class' href=\"{$link}{$strTmp}page={$totalPages}\">>></a></span>";

		return $result;
    }

    /**
     * Get string paging.
     *
     * @param int          $currentPage      		current page.
     * @param int          $totalPages       		total pages recieved from DB.
     * @param int          $numberPageView   		number of num you want to view on the left or right of current page
     *                          example
     *                          $numberPageView = 3
     *                          string paging will be 1 2 3 4 5 6 7.
     * @param string       $url      				url of ajax page.
     * @param string       $method      			POST or GET.
     * @param string       $parameter   			params you send to browser.
     * @param string       $functionNameWhenDone    Javascript function will call after ajax function complete
     * @return  string paging.
     */
/*    ajaxConn.connect('<?=$this->baseUrl?>/ajax/googlesearch', 'POST', 'key=<?=urlencode($this->key)?>&page=' + (p - 1), functionWhenDone);
*/    function getPagingFromAjaxContent($currentPage, $totalPages, $numberPageView, $url, $method = 'POST', $parameter = null, $functionNameWhenDone = 'functionWhenDone', $class = 'paging') {

        if( is_numeric($totalPages) && ($totalPages > 1)) {
            $result = "";

            if( !is_numeric($currentPage) || ($currentPage <= 0) ) {
                $currentPage = 1;
            }

            $i = 0;
            // left paging
            for( $i = 1 ; $i <= $numberPageView; $i++) {
                if( $currentPage - $i > 0 ) {
                    $result = "<a style='margin-left: 5px;' href='#' onclick=\"javascript:ajaxConn.connect('$url/page/".($currentPage - $i). "', '$method', '$parameter', '$functionNameWhenDone')\"><span class='$class'>".($currentPage - $i)."</span></a>" . $result;
                }
            }

            // First
            $result = "<a style='margin-left: 5px;' href='#' onclick=\"javascript:ajaxConn.connect('$url/page/1', '$method', '$parameter', '$functionNameWhenDone')\"><span class='$class'><<</span></a> " . $result;

            // current and right
            for( $i = 0 ; $i <= $numberPageView;$i++) {
                if( $currentPage + $i <= $totalPages ) {
                    if( $i == 0 ) {
                        $result .= "<font color='red'><span class='$class-current' style='margin-left: 5px;'>" .( $currentPage + $i) . "</span></font> ";
                    } else {
                        $result .= "<a style='margin-left: 5px;' href='#' onclick=\"javascript:ajaxConn.connect('$url/page/".($currentPage + $i). "', '$method', '$parameter', '$functionNameWhenDone')\"><span class='$class'>".($currentPage + $i)."</span></a>";
                    }
                }
            }

            // Last
            $result .= "<a style='margin-left: 5px;' href='#' onclick=\"javascript:ajaxConn.connect('$url/page/$totalPages', '$method', '$parameter', '$functionNameWhenDone')\"><span class='$class'>>></span></a>";

            return $result;
        } else {
            return  "";
        }

    }
}